package com.lu.练习0_leetcode.链表;

import org.omg.PortableServer.LIFESPAN_POLICY_ID;

/**
 * https://leetcode-cn.com/problems/reverse-linked-list/
 * @author xiaolu
 * @version 1.0
 * @date 2020/10/29 11:11
 */
public class _206_反转链表 {
    public ListNode reverseList(ListNode head) {

        if (head == null || head.next == null) return head;

        // 反转除头节点之外的节点
        ListNode newHead = reverseList(head.next);
        // 只剩头节点没有反转
        head.next.next = head;
        head.next = null;

        return newHead;
    }

    public ListNode reverseList2(ListNode head) {

        if (head == null || head.next == null) return head;

        // 反转除头节点之外的节点
        ListNode newHead = null;
        while (head != null) {
            ListNode tmp = head.next; // 临时变量
            head.next = newHead;
            newHead = head;
            head = tmp;
        }

        return newHead;
    }
}
